<?php

/**
 * Groups table data gateway
 *
 * @version $Id: Groups.php 73 2010-04-25 00:30:44Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Db_Table_Abstract
 * @package default
 * @subpackage Model
 */
class Default_Model_DbTable_Groups extends Zend_Db_Table_Abstract
{

	protected $_name = 'groups';
	protected $_primary = 'sn';

	protected $_referenceMap	= array(
		'Users'	=>	array(
			'columns'			=>	'username',
			'refTableClass'	=>	'Default_Model_DbTable_Users',
			'refColumns'		=>	'username'
		),
		'AclRoles'	=>	array(
			'columns'			=>	'role_id',
			'refTableClass'	=>	'Default_Model_DbTable_AclRoles',
			'refColumns'		=>	'id'
		),
	);

	public function updateRole($arr,$kind)
	{

		//
		$query = $this->getAdapter()->select()->from(array('a'=>'acl_roles'),
		array('id'))
		->join(array('b'=>'groups'),'a.id=b.role_id',array())
		->where('a.role_kind=?',$kind)
		->where('b.username=?',$arr['username']);
		//echo $query;
		//return ;
		$row = $this->getAdapter()->query($query)->fetch();

		$role_id = $row['id'];
		if ($role_id <> $arr['role_id']) {
			$this->delete("username='{$arr['username']}' AND role_id='$role_id'");
			$this->insert($arr);
		}
	}
}
